//////////////////////////////////////////////////////////////////////////////
// DCTO8DGLOBAL.H - declarations globales
// Author   : Daniel Coulom - danielcoulom@gmail.com
// Web site : http://dcto8.free.fr
//
// This file is part of DCTO8D.
//
// DCTO8D is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DCTO8D is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with DCTO8D. If not, see <http://www.gnu.org/licenses/>.
//
//////////////////////////////////////////////////////////////////////////////
//
//2007-12-28 : v11.0 released

#ifndef DCTO8D_GLOBAL_H
#define DCTO8D_GLOBAL_H

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define XBITMAP 672
#define YBITMAP 216
#define YSTATUS 20

#define LANGUAGE_MAX          2    //nombre de langues differentes
#define TEXT_MAXLENGTH     1024    //longueur maxi d'une chaine de texte
#define MSG_MAX              50    //nombre de textes dans chaque langue
#define TO8D_FILENAME_SIZE  256
#define BOUTON_MAX          105        //nombre de boutons differents

#define KEYBOARDKEY_MAX 84    //nombre de touches du clavier TO8D
#define JOYSTICKKEY_MAX 10    //nombre total de contacts des deux manettes

#define FLAGS SDL_SWSURFACE | SDL_DOUBLEBUF

#define FIELD_ALIGN_32 __attribute__ ((aligned (32)))
#define FIELD_ALIGN_64 __attribute__ ((aligned (64)))

#define TO8D_SAVESTATE_ID  0x65431088
#define X7DA_COLORS        32

/************ 6809/TO8D system stuff *******************************/
typedef struct {
  // Register and global variables
  int dc6809_cycles; //additional cycles
  int dc6809_sync;   //synchronisation flag
  int dc6809_irq;    //irq trigger  (0=inactif)
  int dc6809_firq;   //firq trigger (0=inactif)
  int dc6809_nmi;    //nmi trigger  (0=inactif)
  int16_t dc6809_w;    //dc6809 work register

  // 6809 registers
  char     dc6809_cc FIELD_ALIGN_32;   //condition code
  int16_t  dc6809_pc FIELD_ALIGN_32;    //program counter
  int16_t  dc6809_d  FIELD_ALIGN_32;    //D register
  int16_t  dc6809_x  FIELD_ALIGN_32;    //X register
  int16_t  dc6809_y  FIELD_ALIGN_32;    //Y register
  int16_t  dc6809_u  FIELD_ALIGN_32;    //U register
  int16_t  dc6809_s  FIELD_ALIGN_32;    //S register
  int16_t  dc6809_da FIELD_ALIGN_32;   //direct address (DP register = high byte of direct address)

  // memory
  char car[0x10000]          FIELD_ALIGN_32;   //espace cartouche 4x16K
  char ram[0x80000]          FIELD_ALIGN_32;   //ram 512K
  char port[0x40]            FIELD_ALIGN_32;   //ports d'entree/sortie
  char x7da[X7DA_COLORS]     FIELD_ALIGN_32;   //stockage de la palette de couleurs
  uint16_t pcolor[20]        FIELD_ALIGN_32;   //couleurs BGRA de la palette (pour 8 pixels)

  // banques
  int nvideopage;      //n� page video (00-01)
  int nvideobank;      //n� banque video (00-03)
  int nrambank;        //n� banque ram (00-1f)
  int nrombank;        //n� banque rom (00-07)
  int nsystbank;       //n� banque systeme (00-01)
  int nctrlbank;       //n� banque controleur (00-03)
  // flags cartouche
  int cartype;         //type de cartouche (0=simple 1=switch bank, 2=os-9)
  int carflags;        //bits0,1,4=bank, 2=cart-enabled, 3=write-enabled
  // keyboard, joysticks, mouse
  int touche[KEYBOARDKEY_MAX] __attribute__ ((aligned (8))) ; //etat touches to8d
  int capslock;        //1=capslock, 0 sinon
  int joysposition;    //position des manches
  int joysaction;      //position des boutons d'action
  int xpen, ypen;      //lightpen coordinates
  int penbutton;       //lightpen button state
  //affichage
  int videolinecycle;  //compteur ligne (0-63)
  int videolinenumber; //numero de ligne video affich�e (0-311)
  int vblnumber;       //compteur du nombre de vbl avant affichage
  int vblnumbermax;    //nombre de vbl entre deux affichages
  int displayflag;     //indicateur pour l'affichage
  int bordercolor;     //couleur de la bordure de l'�cran
  //divers
  int sound;           //niveau du haut-parleur
  int mute;            // mute flag
  int timer6846;       //compteur du timer 6846
  int latch6846;       //registre latch du timer 6846
  int keyb_irqcount;   //nombre de cycles avant la fin de l'irq clavier
  int timer_irqcount;  //nombre de cycles avant la fin de l'irq timer

  int pause6809;        //processor pause state
  int report;           //nombre de milliemes de cycle a reporter
  int globforcerefresh;

} TO8D_System_t;
extern TO8D_System_t *psys;
void TO8D_save_state(char *statename);
void TO8D_restore_state(char *statename);
char  Mgetto8d(unsigned short a);
void  Mputto8d(unsigned short a, char c);

/* memory access C = 1 byte, W = 2 bytes */
#define GETC(x)   Mgetto8d(x)
#define PUTC(x,y) Mputto8d(x,y)
#define GETW(x)   (Mgetto8d(x)<<8|(Mgetto8d(x+1)&0xff))
#define PUTW(x,y) {Mputto8d(x,y>>8);Mputto8d(x+1,y);}


/******************* Otherstuff *****************************/
typedef struct
{
 int x;         //ordonnee du coin haut-gauche
 int y;         //abscisse du coin haut-gauche
 int w;         //largeur en pixels
 int h;         //hauteur en pixels
 char *text;    //texte a editer
} dialogeditbox;

typedef struct
{
 //char name[12]; //nom du bouton
 int x;         //ordonnee du coin haut-gauche
 int y;         //abscisse du coin haut-gauche
 //int w;         //largeur en pixels
 //int h;         //hauteur en pixels
 int n;         //numero de la surface SDL
} dialogbutton;

typedef struct
{
 char name[12]; //nom du bouton
 int w;         //largeur en pixels
 int h;         //hauteur en pixels
} button;

//fonctions
void Createdialogbox(int w, int h);
void Displayscreen();
void Drawdesassbox();
void Drawk7index();
void Drawjoystickbox();
void Drawkeyboardbox();
void Drawpopuptable(int n, int x, int y);
void Drawoptionbox();
void Erreur(int n);
void Hardreset();
void Info(int i);
void Initprog();
void Resizescreen(int x, int y);
void SDL_error(int n);
void Setoption(int i);

/* Exported/shared functions */
char *TO8D_get_best_file_from_device(void);

#endif

